package com.shop.operator.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.domain.PageResult;
import com.shop.domain.SpecificationCustom;
import com.shop.model.Specification;
import com.shop.operator.service.SpecificationService;

@Controller
@RequestMapping("/specificationAction")
public class SpecificationAction {

	@Resource(name="specificationSer")
	private SpecificationService specificationService;
	
	/**
	 * http:localhost:8080/Shopping/specificationAction/pageSpecification.action
	 * 跳转到规格管理页面
	 * @return
	 */
	@RequestMapping("/pageSpecification")
	public String pageSpecification(){
		return "operator/admin/specification";
	}
	
	/**
	 * http://localhost:8080/Shopping/specificationAction/findAll.action
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public List<Specification> findAll()throws Exception{
		List<Specification> specifications = this.specificationService.findAll();
		return specifications;
	}
	
	/**
	 * http://localhost:8080/Shopping/specificationAction/findBySpecName.action
	 * @param specName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/search")
	@ResponseBody
	public PageResult search(String specName,int page,int size) throws Exception{
		return this.specificationService.findPage(specName, page, size);
	}
	
	/**
	 * http://localhost:8080/Shopping/specificationAction/findPage.action?page=1&size=1
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping("/findPage")
	@ResponseBody
	public PageResult findPage(int page,int size){
		return specificationService.findPage(page, size);
	}
	
	/**
	 * @param specificationCustom
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(@RequestBody SpecificationCustom specificationCustom){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Specification> lists = this.specificationService.findName(specificationCustom.getSpecification().getSpecName());
			if(lists.size()>0){
				map.put("success", false);
				map.put("message", "该规格已存在,请重新输入!!!!");
			}else{
				this.specificationService.add(specificationCustom);
				map.put("success", true);
				map.put("message", "添加成功!!!");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			map.put("success", false);
			map.put("message", "添加失败!!!");
		}
		return map;
	}
	
	/**
	 * http://localhost:8080/Shopping/specificationAction/findOne.action
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findOne")
	@ResponseBody
	public SpecificationCustom findOne(long id)throws Exception{
		return this.specificationService.findOne(id);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(@RequestBody SpecificationCustom specificationCustom){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//根据要修改数据的id，查找数据库中的数据
			SpecificationCustom specificationCustom_db = this.specificationService.findOne(specificationCustom.getSpecification().getId());
			//判断修改前后的内容是否相同，返回true表可以修改
			if(specificationCustom.equals(specificationCustom_db)){
				this.specificationService.update(specificationCustom);
				map.put("success", true);
				map.put("message", "修改成功!!!");
			}else{
				map.put("success", false);
				map.put("message", "您修改后的数据已存在，请重新修改!!!");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			map.put("success", false);
			map.put("message", "修改失败!!!");
		}
		return map;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(long[] ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			this.specificationService.delete(ids);
			map.put("success", true);
			map.put("message", "删除成功!!!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", true);
			map.put("message", "删除失败!!!");
		}
		return map;
	}
	
	
	
}
